package com.quan.server.endpoint;

import com.quan.server.Const;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.regex.Pattern;

@Controller
public class SinglePageApplicationController {
    private static Logger logger = LoggerFactory.getLogger(SinglePageApplicationController.class);
    private static final String ForwardIndexWeixinmp = "forward:/weixinmp/index.html";

    @RequestMapping(
        value = {
            "/manager/",
        })
    public String redirectManagerIndex() {
        return "forward:/manager/index.html";
    }

    @RequestMapping(
        value = {
            "/manager/{path0:^[a-zA-Z0-9]*$}",
        })
    public String redirectManagerIndex(@PathVariable String path0) {
        return "forward:/manager/index.html";
    }

    @RequestMapping(
        value = {
            "/manager/{path1:^[a-zA-Z0-9]*$}/{path2:^[a-zA-Z0-9]*$}"
        })
    public String redirectManagerIndex(@PathVariable String path1, @PathVariable String path2) {
        return "forward:/manager/index.html";
    }

    @RequestMapping(
        value = {
            Const.ContextWeixinmp + "/",
        })
    public String redirectWeixinmpIndex() {
        return ForwardIndexWeixinmp;
    }

    @RequestMapping(
        value = {
            Const.ContextWeixinmp + "/{path0:^[a-zA-Z0-9]*$}",
        })
    public String redirectWeixinmpIndex(@PathVariable String path0) {
        return ForwardIndexWeixinmp;
    }

    @RequestMapping(
        value = {
            Const.ContextWeixinmp + "/{path1:^[a-zA-Z0-9]*$}/{path2:^[a-zA-Z0-9]*$}"
        })
    public String redirectWeixinmpIndex(@PathVariable String path1, @PathVariable String path2) {
        return ForwardIndexWeixinmp;
    }

    public static void main(String[] args) {
        isMatch("aaaahello");
        isMatch("rest");
        isMatch("index.html");
        isMatch("XG2017101408241890");
    }

    public static void isMatch(String content) {
        String pattern = "^(?!rest)[a-zA-Z0-9]*$";
        boolean isMatch = Pattern.matches(pattern, content);
        System.out.println("testString:" + content + ", isMatch:" + isMatch);
    }
}
